package com.ruoyi.issues.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResultPlus;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.issues.entity.Issues;
import com.ruoyi.issues.entity.query.IssueQuery;
import com.ruoyi.issues.entity.vo.IssueVo;
import com.ruoyi.issues.service.IssuesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.BeanUtils;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 建议反馈表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-14
 */
@Api("反馈")
@RestController
@RequestMapping("/issues")
public class IssuesController extends BaseController {

    @Autowired
    private IssuesService issuesService;

    //1 游客添加issue
    @ApiOperation("添加issues")
    @PostMapping("addIssueEveryone")
    @Log(title = "issues管理", businessType = BusinessType.INSERT)
    public AjaxResult addIssue(@RequestBody IssueVo issue){
        Issues issues = new Issues();
        BeanUtils.copyProperties(issue,issues);
        boolean save = issuesService.save(issues);
        return toAjax(save);
    }

    //1 增加issues
    @ApiOperation("添加issues")
    @PostMapping("addIssues")
    @PreAuthorize("@ss.hasPermi('bmService:issues:add')")
    @Log(title = "issues管理", businessType = BusinessType.INSERT)
    public AjaxResult addIssues(@RequestBody IssueVo issue){
        Issues issues = new Issues();
        BeanUtils.copyProperties(issue,issues);
        boolean save = issuesService.save(issues);
        return toAjax(save);
    }


    //2 批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("removeIssues/{issuesIds}")
    @PreAuthorize("@ss.hasPermi('bmService:issues:remove')")
    @Log(title = "issues管理", businessType = BusinessType.DELETE)
    public AjaxResult removeIssues(@PathVariable String[] issuesIds){
        boolean b = issuesService.removeByIds(Arrays.asList(issuesIds));
        return toAjax(b);
    }


    //3 根据id更新
    @ApiOperation("根据id更新")
    @PutMapping("update")
    @PreAuthorize("@ss.hasPermi('bmService:issues:update')")
    @Log(title = "issues管理", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody IssueVo issue){
        Issues issues = new Issues();
        BeanUtils.copyProperties(issue,issues);
        boolean b = issuesService.updateById(issues);
        return toAjax(b);
    }

    //4 多条件组合查询分页
    @ApiOperation("多条件组合查询分页")
    @PostMapping("/getList/{current}/{limit}")  // request body 必须配合post请求使用
    @PreAuthorize("@ss.hasPermi('bmService:issues:getList')")
    public AjaxResultPlus getList(@PathVariable String current, @PathVariable String limit,@RequestBody IssueQuery issueQuery){
        System.out.println("issueQuery = " + issueQuery);
        Page<Issues> listPage = issuesService.getList(current, limit, issueQuery); // 返回page对象
        long total = listPage.getTotal();
        List<Issues> issuesList = listPage.getRecords();

        return AjaxResultPlus.success().data("total",total).data("issuesList",issuesList);
    }

    //5 根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("getOne/{issuesId}")
    @PreAuthorize("@ss.hasPermi('bmService:issues:getOne')")
    public AjaxResult getOne(@PathVariable String issuesId){
        Issues issues = issuesService.getById(issuesId);
        return AjaxResult.success(issues);
    }
}

